﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

using System.Data.SqlClient;
using System.Web.Configuration;
using System.Security.Cryptography;
using System.Text;

namespace HiViLicenseManage
{
	public partial class WebForm1 : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			try
			{
				if (Session["UserMember"] == null)
					Response.Redirect("~/ManageUserMember.aspx", false);
				else
				{
					SucsessChangePassword.InnerText = "";
					if (!IsPostBack)
					{
						LoadInfomation();
					}
				}
			}
			catch
			{
				Response.Redirect("~/Error.aspx", false);
			}
		}

		private void LoadInfomation()
		{
			SqlConnection appConn = new SqlConnection(WebConfigurationManager.ConnectionStrings["appconn"].ConnectionString);
			appConn.Open();

			SqlCommand sqlComm = new SqlCommand("SELECT * FROM Agent WHERE UPPER(AgentID) = @AgentID", appConn);
			sqlComm.CommandType = CommandType.Text;
			sqlComm.Parameters.Add("@AgentID", SqlDbType.VarChar).Value = Session["UserMember"].ToString().ToUpper();
			SqlDataAdapter sqlDA = new SqlDataAdapter();
			sqlDA.SelectCommand = sqlComm;
			DataTable dtAgent = new DataTable();
			sqlDA.Fill(dtAgent);

			if (dtAgent.Rows.Count > 0)
			{
				txtAgentID.Text = Session["UserMember"].ToString();
				txtAgentName.Text = dtAgent.Rows[0]["AgentName"].ToString();
				txtPhone.Text = dtAgent.Rows[0]["Phone"].ToString();
				txtEmail.Text = dtAgent.Rows[0]["Email"].ToString();
				txtCompany.Text = dtAgent.Rows[0]["Company"].ToString();
				txtAddress.Text = dtAgent.Rows[0]["Address"].ToString();
				ckActive.Checked = Convert.ToBoolean(dtAgent.Rows[0]["Status"]);
			}

			SqlCommand sqlComm1 = new SqlCommand("SELECT FK_SoftwareID, SoftwareName, NumberKey, Status FROM vwAgentKey WHERE UPPER(FK_AgentID) = @FK_AgentID", appConn);
			sqlComm1.CommandType = CommandType.Text;
			sqlComm1.Parameters.Add("@FK_AgentID", SqlDbType.VarChar).Value = Session["UserMember"].ToString().ToUpper();
			SqlDataAdapter sqlDA1 = new SqlDataAdapter();
			sqlDA1.SelectCommand = sqlComm1;
			DataTable dtSoftware = new DataTable();
			sqlDA1.Fill(dtSoftware);

			cklSoftware.DataSource = dtSoftware;
			cklSoftware.DataValueField = "FK_SoftwareID";
			cklSoftware.DataTextField = "SoftwareName";
			cklSoftware.DataBind();

			foreach (ListItem item in cklSoftware.Items)
			{
				for (int i = 0; i < dtSoftware.Rows.Count; i++)
				{
					if (item.Value == dtSoftware.Rows[i]["FK_SoftwareID"].ToString())
					{
						if (dtSoftware.Rows[i]["Status"].ToString() == "True")
							item.Selected = true;
						else
							item.Selected = false;
						break;
					}
				}
			}

			if (dtSoftware.Rows.Count > 0)
				txtNumberKey.Text = dtSoftware.Rows[0]["NumberKey"].ToString();

			appConn.Close();
		}

		protected void btnOk_Click(object sender, EventArgs e)
		{
			SqlConnection appConn = new SqlConnection(WebConfigurationManager.ConnectionStrings["appconn"].ConnectionString);
			appConn.Open();

			string sCom = "UPDATE Agent SET ";
			if (txtPassword.Text.Trim().Length > 0)
			{
				sCom = sCom + "Password = @Password, ";
			}
			sCom = sCom + "AgentName = @AgentName, ";
			sCom = sCom + "Phone = @Phone, ";
			sCom = sCom + "Email = @Email, ";
			sCom = sCom + "Company = @Company, ";
			sCom = sCom + "Address = @Address, ";
			sCom = sCom + "Status = @Status ";
			sCom = sCom + "WHERE UPPER(AgentID) = @AgentID";

			SqlCommand sqlComm = new SqlCommand(sCom, appConn);
			sqlComm.CommandType = CommandType.Text;
			if (txtPassword.Text.Trim().Length > 0)
			{
				sqlComm.Parameters.Add("@Password", SqlDbType.VarChar).Value = Enc.Encrypt(txtPassword.Text.Trim(), txtAgentID.Text.ToUpper());
			}
			sqlComm.Parameters.Add("@AgentName", SqlDbType.NVarChar).Value = txtAgentName.Text;
			sqlComm.Parameters.Add("@Phone", SqlDbType.VarChar).Value = txtPhone.Text;
			sqlComm.Parameters.Add("@Email", SqlDbType.VarChar).Value = txtEmail.Text;
			sqlComm.Parameters.Add("@Company", SqlDbType.NVarChar).Value = txtCompany.Text;
			sqlComm.Parameters.Add("@Address", SqlDbType.NVarChar).Value = txtAddress.Text;
			sqlComm.Parameters.Add("@Status", SqlDbType.Bit).Value = ckActive.Checked;
			sqlComm.Parameters.Add("@AgentID", SqlDbType.VarChar).Value = txtAgentID.Text.ToUpper();
			sqlComm.ExecuteNonQuery();

			foreach (ListItem item in cklSoftware.Items)
			{
				SqlCommand sqlComm2 = new SqlCommand("UPDATE AgentKey SET NumberKey = @NumberKey, Status = @Status  WHERE UPPER(FK_AgentID) = @FK_AgentID AND FK_SoftwareID = @FK_SoftwareID", appConn);
				sqlComm2.CommandType = CommandType.Text;
				sqlComm2.Parameters.Add("@NumberKey", SqlDbType.Int).Value = Convert.ToInt32(txtNumberKey.Text);
				sqlComm2.Parameters.Add("@Status", SqlDbType.Bit).Value = item.Selected;
				sqlComm2.Parameters.Add("@FK_AgentID", SqlDbType.VarChar).Value = txtAgentID.Text.Trim().ToUpper();
				sqlComm2.Parameters.Add("@FK_SoftwareID", SqlDbType.VarChar).Value = item.Value;
				sqlComm2.ExecuteNonQuery();
			}

			SucsessChangePassword.InnerText = "Lưu thông tin thay đổi thành công!";
			
			appConn.Close();
			
			Response.Redirect("~/ManageUserMember.aspx", false);
		}
	}
}
